require(["config"],function(){

    require(["jquery", "jquery.validation", "additional.methods", "md5"], function(){

        $(function(){


                $("#myfrom").validate({
                    submitHandler : function(){
                        //使用ajax

                        $.ajax({
                            url : "http://127.0.0.1/Shopping/server/register.php",
                            type : "post",
                            data : {
                                uname:$("[name='uname']").val(),
                                upwd:$.md5($("[name='upwd']").val()),
                                ureallyname:$("[name='ureallyname']").val(),
                                utel:$("[name='utel']").val(),
                                uaddress:$("[name='uaddress']").val(),
                            },
                            dataType : "json"
                        }).then(function(res){
                            console.log(res);
                            if(res.status == 1){
                                if(confirm("注册成功,是否需要登录?")){
                                    window.location.assign("login.html");
                                }
                            } else {
                                alert(res.msg);
                            }
                        })

                        return false;
                    },

                    rules : {
                        uname : {
                            'required' : true,
                            'rangelength' : [6, 18],
                            'checkUser' : true,
                            remote : {
                                type : "get",
                                url : "http://127.0.0.1/Shopping/server/IsExistUser.php",
                            }
                        },
                        upwd : {
                            'required' : true,
                            'rangelength' : [6, 18]
                        },
                        upwd1 : {
                            equalTo : "#upwd"
                        },

                        ureallyname : {
                            'required' : true,
                            'checkChinese' : true,
                        },


                        utel : {
                            'required' : true,
                            'rangelength' : [11, 11]
                        },
                        uaddress : 'required'

                    },
                    messages : {
                        uname : {
                            required : "用户名必填",
                            rangelength : "用户名长度必须6-18之间",
                            remote : "该用户名已经存在"
                        },
                        upwd : {
                            required : "密码必填",
                            rangelength : "密码长度必须6-18之间"
                        },
                        upwd1 : {
                            equalTo : '2次密码不一致'
                        },
                        ureallyname : {
                            required : "真实名字必填"
                        },
                        utel : {
                            required : "手机号必填",
                            rangelength : "手机不合法"
                        },
                        uaddress : {
                            required : "地址必填"
                        }
                    }

                })


                $.validator.addMethod("checkUser", function(val){
                    return /^[a-zA-Z]+\w/.test(val);
                }, "用户名只能是数字,字母");

                $.validator.addMethod("checkChinese", function(value){
                    return /^[\u2E80-\u9FFF]+$/.test(value);
                }, "真实名字必须是中文")

            })

        })

    })

